package com.qiantu.phone.manager;

import android.util.Log;

import com.hjq.http.model.FileContentResolver;
import com.qiantu.phone.BuildConfig;
import com.obs.services.ObsClient;
import com.obs.services.exception.ObsException;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;

import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;

public class OBSManager {
    private static String ak = "LHOWRTVLI59KGKPOWE0N";
    private static String sk = "SsvGiA1HJBvB6dErvF7ojdYOCJPteZ8v4DqWoas4";

    public static Disposable updateImage(String path, File file, OBSCallBack callBack){
        Disposable disposable = Observable.create(new ObservableOnSubscribe<String>() {
            @Override
            public void subscribe(ObservableEmitter<String> emitter) throws Exception {
                // 您的工程中可以只保留一个全局的ObsClient实例
                // ObsClient是线程安全的，可在并发场景下使用
                ObsClient obsClient = null;
                String bucketName = BuildConfig.BUCKET_NAME;
                String objectName;
                if(BuildConfig.BUILD_TYPE.equals("debug")){
                    objectName = "dev/app/images/"+path;
                }else{
                    objectName = "production/app/images/"+path;
                }
                String imageUrl = null;
                try {
                    // 创建ObsClient实例
                    obsClient = new ObsClient(ak, sk, BuildConfig.END_POINT);
                    // 调用接口进行操作，例如上传对象，其中localfile为待上传的本地文件路径，需要指定到具体的文件名
                    if (file instanceof FileContentResolver){
                        FileContentResolver fileContentResolver = (FileContentResolver) file;
                        InputStream inputStream = fileContentResolver.openInputStream();
                        obsClient.putObject(bucketName, objectName, inputStream);
                    }else {
                        obsClient.putObject(bucketName, objectName, file);
                    }
                    imageUrl = BuildConfig.OBS_BASE + objectName;
                } catch (ObsException e) {
                    Log.e("PutObject", "Response Code: " + e.getResponseCode());
                    Log.e("PutObject", "Error Message: " + e.getErrorMessage());
                    Log.e("PutObject", "Error Code:       " + e.getErrorCode());
                    Log.e("PutObject", "Request ID:      " + e.getErrorRequestId());
                    Log.e("PutObject", "Host ID:           " + e.getErrorHostId());
                    imageUrl = null;
                } finally {
                    // 关闭ObsClient实例，如果是全局ObsClient实例，可以不在每个方法调用完成后关闭
                    // ObsClient在调用ObsClient.close方法关闭后不能再次使用
                    if (obsClient != null) {
                        try {
                            obsClient.close();
                        } catch (IOException e) {
                        }
                    }
                    emitter.onNext(imageUrl);
                }
            }
        }).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<String>() {
                    @Override
                    public void accept(String result) throws Exception {
                        if (result!=null){
                            callBack.onSuccess(result);
                        }else {
                            callBack.onFail();
                        }
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        callBack.onFail();
                    }
                });
        return disposable;
    }

    public interface OBSCallBack{
        void onSuccess(String url);
        void onFail();
    }

}
